const {
    mysql
  } = require('../../database/mysql')
  const Job = require('../../model/job')
  const Uuid = require('../../utils/uuid')
  
  class JobService {
    //添加新球场
    static async newJob({
      ctx
    }) {
      let newJobInfo = {}
      newJobInfo = ctx.request.body
      const job = new Job(newJobInfo)
      job.id = new Uuid().uuid
      job.createdDate = new Date()
      const result = await mysql('job').insert(job.getData().job)
      return result[0] === 0 ? {
        result: true,
        job: job.getData().job
      } : {
        result: false
      }
    }
  
    //获取所有球场信息
    static async getAllJob({ctx}) {
      let newJobInfo = {}
      newJobInfo = ctx.request.body
      console.log(newJobInfo,"666")

      const job = new Job(newJobInfo)
      console.log(job.getData().jobWithNoNull,"666")
      const result = await mysql('job').where(job.getData().jobWithNoNull).select().orderBy('created_date', 'desc')
      const JobsInfo = []
      result.forEach((JobInfo) => {
        JobsInfo.push(JobInfo)
      })
      console.log(JobsInfo,"66")

      let JobsInfo2 = []
      for(let kk  in JobsInfo){
        console.log(JobsInfo[kk].company_id,"66")
        const company = await mysql('company').where({id:JobsInfo[kk].company_id}).select()
        JobsInfo2.push({...JobsInfo[kk],company:company[0]||null})
      }

      return JobsInfo2
    }

    // 根据id找用户
  static async getJobById({ id }) {
    console.log(id)
  
      const [jobInfo] = await mysql('job')
        .where({ id })
        .select()
    if(jobInfo) {
      const job = new Job(jobInfo)
      return job.getData()
    }else{
      return false
    }
  }
  
      //获取所有球场信息(带订单)
      static async getJobWithOrder({
        day1,day2,day3
     }) {
        const result = await mysql('job').select('job.*',
        {'day1':await mysql('order').select().where('job_id','=','job.id').andWhere({date:day1})
        },
        {'day2':
          await mysql('order').select().where('job_id','=','job.id').andWhere({date:day2})
        },
        {'day3':
          await mysql('order').select().where('job_id','=','job.id').andWhere({date:day3})
        }
        )
        const JobsInfo = []
        result.forEach((JobInfo) => {
          JobsInfo.push(JobInfo)
        })
        return JobsInfo
      }
    // 删除某个球场
    static async delJob({
      id
    }) {
      const result = await mysql('job')
        .where({
          id
        })
        .del()
  
      return result === 1
    }
    // 球场信息更新
    static async setJob({
      ctx,
      id
    }) {
      let updateJobInfo = {}
      updateJobInfo = ctx.request.body
      const job = new Job(updateJobInfo)
      const updateInfo = job.getData().jobWithNoNull
      Reflect.deleteProperty(updateInfo, 'id')
      Reflect.deleteProperty(updateInfo, 'created_date')
      Reflect.deleteProperty(updateInfo, 'updated_date')
      const result = await mysql('job').where({
        id
      }).update(updateInfo)
      return result === 1
    }
  }
  
  
  module.exports = JobService